package com.horoscope.astrology.zodiac.signs.free.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.activity.ArticleDetailActivity
import com.horoscope.astrology.zodiac.signs.free.activity.MainActivity
import com.horoscope.astrology.zodiac.signs.free.adapter.recyclerHolder.NativeADViewHolder
import com.horoscope.astrology.zodiac.signs.free.bean.ArticleResultDataBean
import com.horoscope.astrology.zodiac.signs.free.bean.NativeAdBean
import com.horoscope.astrology.zodiac.signs.free.utils.EventType
import com.horoscope.astrology.zodiac.signs.free.utils.FlurryEventUtil
import com.horoscope.astrology.zodiac.signs.free.utils.ImageLoader
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ADConfig
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ad_business.ADNativeManager

/**
 * Article 四个列表的 Adapter
 * 对应 ArticleSubFragment
 * */
class ArticleAdapter(private val mContext: Context) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var mDataList = ArrayList<ArticleResultDataBean>()
    private var loadFinished: Boolean = false
    private var mTitle = ""

    fun setData(list: ArrayList<ArticleResultDataBean>) {
        mDataList.addAll(0, list)
        notifyItemRangeChanged(0, list.size)

    }


    fun setTitleOfFragment(title: String) {
        mTitle = title
    }


    fun setMoreData(list: ArrayList<ArticleResultDataBean>) {
        mDataList.addAll(list)
        notifyItemRangeChanged(itemCount, list.size)
    }


    override fun getItemCount(): Int {
        return if (mDataList.isEmpty()) {
            0
        } else {
            mDataList.size + 1
        }
    }

    //var bean = ArticleResultDataBean()
    // 设置数据并显示
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, p1: Int) {
        when (holder) {
            is ArticleViewHolder -> {
                val bean = mDataList[p1]
                when (bean.images.size) {
                    3 -> {
                        ImageLoader.loadImage(holder.image1, bean.images[0])
                        ImageLoader.loadImage(holder.image2, bean.images[1])
                        ImageLoader.loadImage(holder.image3, bean.images[2])
                    }
                    2 -> {
                        ImageLoader.loadImage(holder.image1, bean.images[0])
                        ImageLoader.loadImage(holder.image2, bean.images[1])
                    }
                    1 -> ImageLoader.loadImage(holder.image1, bean.images[0])
                }
                holder.title.text = bean.title
                holder.itemView.setOnClickListener {
                    // 在文章列表页，点击了文章
                    FlurryEventUtil.putDataToEvent(EventType.TYPE_ARTICLE_CLICK, null, null)
                    mContext.startActivity(
                        ArticleDetailActivity.generateIntent(mContext, bean.title, bean.paragraphs)
                    )
                }

            }
            is ArticleViewHolderWithOnePic -> {
                if (p1 < mDataList.size) {
                    val bean = mDataList[p1]
                    holder.title.text = bean.title
                    if (bean.images.size > 0) {
                        ImageLoader.loadImage(holder.image1, bean.images[0])
                    }
                    holder.itemView.setOnClickListener {
                        mContext.startActivity(
                            ArticleDetailActivity.generateIntent(
                                mContext,
                                bean.title,
                                bean.paragraphs
                            )
                        )
                        // 在文章列表页，点击了文章
                        FlurryEventUtil.putDataToEvent(EventType.TYPE_ARTICLE_CLICK, null, null)
                    }
                }


            }
            is FooterViewHolder -> {
                handleFooterViewHolder(holder)
            }
            is NativeADViewHolder -> {
                if (isCurrentPage()) {
                    val bean = mDataList[p1]
                    if (bean.type == 1) {
                        if (bean.nativeAdBean == null) {
                            val nativeAdBean =
                                ADNativeManager.mInstance.getNativeAdBean("Native_List")
                            bean.nativeAdBean = nativeAdBean
                            handleNativeAd(nativeAdBean, holder, p1,"Native_List")
                        } else {
                            handleNativeAd(bean.nativeAdBean, holder, p1,"")
                        }
                    } else {
                        holder.inVisibility()
                    }
                } else {
                    holder.inVisibility()
                }
            }
        }
    }

    private fun isCurrentPage(): Boolean {
        if (mContext is MainActivity) {
            if (mTitle == mContext.getArticleCurrentTitle()) {
                return true
            }
        }
        return false
    }

    fun setLoadFinished(loadFinished: Boolean) {
        this.loadFinished = loadFinished
        notifyDataSetChanged()
        notifyItemChanged(itemCount)
    }


    // 对多个布局进行分辨 默认一张图片
    override fun getItemViewType(position: Int): Int {
        return if (position == mDataList.size) {
            BOTTOM
        } else {
            if (mDataList[position].type == 1) {
                AD
            } else {
                mDataList[position].images.size
            }
        }
    }

    // 具体的为每一种类型引入其布局
    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): RecyclerView.ViewHolder {

        return when (p1) {
            AD -> {
                val view = LayoutInflater.from(mContext).inflate(R.layout.ad_layout, parent, false)
                return NativeADViewHolder(mContext, view)
            }
            3 -> {//三个图片
                val view =
                    LayoutInflater.from(mContext).inflate(R.layout.item_article, parent, false)
                ArticleViewHolder(view)
            }
            BOTTOM -> {
                val view = LayoutInflater.from(mContext)
                    .inflate(R.layout.list_view_footer_comment, parent, false)
                FooterViewHolder(view)
            }
            else -> {
                val view = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_article_with_one_pic, parent, false)
                ArticleViewHolderWithOnePic(view)
            }
        }

    }

    private fun handleNativeAd(
        nativeAdBean: NativeAdBean?,
        holder: NativeADViewHolder,
        position: Int,
        text: String
    ) {
        if (nativeAdBean != null) {
            holder.changeAD(nativeAdBean.adInfo.advertiser)
            when (nativeAdBean.adInfo.advertiser) {
                ADConfig.FB -> {
                    if (nativeAdBean.facebookNativeAD != null) {
                        holder.inflateFacebookAd(nativeAdBean.facebookNativeAD, "Custom_Medium",text)
                        holder.visibility()
                    } else {
                        holder.inVisibility()
                    }
                }
                ADConfig.ADMOB -> {
                    if (nativeAdBean.googleNativeAd != null) {
                        holder.inflateAdMobAd(nativeAdBean.googleNativeAd!!, "Custom_Small")
                        holder.visibility()
                    } else {
                        holder.inVisibility()
                    }
                }
            }

        } else {//没有拿到广告，从缓存中取
            holder.inVisibility()
        }
    }

    companion object {
        const val BOTTOM = 0x13
        const val AD = 0x14
    }


    /**
     * 所对应的具体布局
     * **/
    private fun handleFooterViewHolder(holder: FooterViewHolder) {
        if (loadFinished) {
            if (holder.itemView.visibility != View.VISIBLE) {
                holder.itemView.visibility = View.VISIBLE
            }
            if (holder.progressBar.visibility != View.INVISIBLE) {
                holder.progressBar.visibility = View.INVISIBLE
            }
            holder.textView.visibility = View.VISIBLE
            holder.textView.text = mContext.getString(R.string.label_bottom)
            holder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.white_3))
        } else {
            if (holder.progressBar.visibility != View.VISIBLE) {
                holder.progressBar.visibility = View.VISIBLE
            }
            holder.textView.visibility = View.GONE
            //holder.textView.text = mContext.getString(R.string.dialog_progress_loading)
            //holder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.white))
        }
    }

    inner class ArticleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val title = itemView.findViewById<TextView>(R.id.text_view_tips_article)!!
        val image1 = itemView.findViewById<ImageView>(R.id.image_view_1)!!
        val image2 = itemView.findViewById<ImageView>(R.id.image_view_2)!!
        val image3 = itemView.findViewById<ImageView>(R.id.image_view_3)!!
    }

    inner class ArticleViewHolderWithOnePic(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val title = itemView.findViewById<TextView>(R.id.text_view_tips_article)!!
        val image1 = itemView.findViewById<ImageView>(R.id.image_view_1)!!
    }

    inner class FooterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val progressBar: ProgressBar = itemView.findViewById(R.id.list_view_foot_progress)
        val textView: TextView = itemView.findViewById(R.id.list_view_foot_more)
    }

}